package com.gupao.javavip.quartz.config;

import com.gupao.javavip.quartz.job.QuartzJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author King
 */
@Configuration
public class ServiceConfiguration {

    @Value("${service.cron.schedule}")
    private String schedule;

    /**
     * JobDetail 用于指定定时任务具体的类
     */
    @Bean
    public JobDetail quartzTaskServiceJobDetail() {
        return JobBuilder.newJob(QuartzJob.class).withIdentity("QuartzJob").storeDurably().build();
    }

    /**
     * Trigger 用于指定定时任务触发的机制
     */
    @Bean
    public Trigger quartzTaskServiceTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(schedule);
        return TriggerBuilder.newTrigger().forJob(quartzTaskServiceJobDetail())
                .withIdentity("QuartzJob")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
